package com.wave.wavesystem.ai.mapper;

import com.mybatisflex.core.BaseMapper;
import com.wave.wavesystem.ai.pojo.entity.RagPostChunk;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RagPostChunkMapper extends BaseMapper<RagPostChunk> {


    @Select("""
    SELECT c.postId   AS postId,
           c.sliceText AS sliceText,
           c.embedding <=> STRING_TO_VECTOR(#{vec}) AS scoreVec
    FROM rag_post_chunk c
    WHERE (MATCH(c.sliceText) AGAINST(#{kw} IN NATURAL LANGUAGE MODE) > 0
           OR c.embedding <=> STRING_TO_VECTOR(#{vec}) < 0.3)
    ORDER BY scoreVec ASC
    LIMIT #{topK}
    """)
    List<RagPostChunk> searchHybrid(@Param("kw") String kw,
                                    @Param("vec") byte[] vec,
                                    @Param("topK") int topK);

}
